package com.shenghua.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * 修改图片映射地址
 * Created by Administrator on 2018/12/19.
 */
@Configuration
public class StaticFilesConfig implements WebMvcConfigurer {
    @Value("${temp-path}")
    private  String tempPath;

    @Value("${upload-path}")
    private  String uploadPath;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 图片映射路径
        /*File p = null; // 文件存到classpth路径下
        try {
            p = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if(!p.exists()){
            p = new File("");
        }
        String basePath = p.getAbsolutePath();*/
        registry.addResourceHandler("/sh/temp/**").addResourceLocations("file:"+tempPath);
        //registry.addResourceHandler("/sh/temp/**").addResourceLocations("file:"+basePath+"/temp/");
        registry.addResourceHandler("/sh/upload/**").addResourceLocations("file:"+uploadPath);
        //registry.addResourceHandler("/sh/upload/**").addResourceLocations("file:"+basePath+"/upload/");
    }
}
